package com.example.wanxiang.controller;


import com.example.wanxiang.entity.Comment;
import com.example.wanxiang.service.CommentService;
import jakarta.annotation.Resource;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    @GetMapping("/{articleId}")
    public List<Comment> findByArticleId(@PathVariable String articleId,
                                         @RequestParam(defaultValue = "0") int page,
                                         @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return commentService.findByArticleId(articleId, pageable);
    }

    @PostMapping("/{articleId}")
    public Comment create(@PathVariable String articleId, @RequestBody Comment comment) {
        comment.setArticleId(articleId);
        return commentService.save(comment);
    }

    @PutMapping("/{id}")
    public Comment update(@PathVariable String id, @RequestBody Comment comment) {
        comment.setId(id);
        return commentService.save(comment);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id) {
        commentService.deleteById(id);
    }

    @GetMapping("/{id}")
    public Comment findById(@PathVariable String id) {
        return commentService.findById(id);
    }

}



